package com.huixue.cn.ztej.service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huixue.cn.ztej.bean.Resource;
import com.huixue.cn.ztej.dao.ResourceMapper;

@Service
public class SysResourceService {

	@Autowired
	private ResourceMapper resourceDao;
	
	public List<Resource> findall(){
		List<Resource> list1 = new ArrayList<Resource>();
		List<Resource> list = resourceDao.findall();
		for(Resource rs:list){
			if(rs.getParentId()==-1){
				list1.add(rs);
			}
		}
		
		if(list1.size()>0 && list.size()>0){
			for(Resource rs:list1){
				getchildren(rs, list);
			}
		}
		
		return list1;
	}
	
	public Resource getchildren(Resource resource,List<Resource> list){
		if(list.size()>0 && resource.getType()==1){
			List<Resource> childlist = new ArrayList<Resource>();
			resource.setHavechild(0);
			for(Resource rs:list){
				if(rs.getParentId() ==resource.getResourceId()){
					childlist.add(rs);
					if(rs.getType()==1 && list.size()>0){
						rs = getchildren(rs, list);
					}
				}
			}
			if(childlist.size()>0){
				resource.setHavechild(1);
				resource.setList(childlist);
			}
			
		}
		return resource;
	}
	
	public void insert(Resource resource){
		resourceDao.insert(resource);
	}
	
	public void update(Resource resource){
		resourceDao.update(resource);
	}
	
	public void delete(int resourceId){
		Resource resource =new Resource();
		resource.setResourceId(resourceId);
		resourceDao.delete(resource);
	}
	
	public Resource selectById(String id){
		return resourceDao.selectById(id);
	}
	
	public List<Resource> findParent(){
		return resourceDao.findParent();
	}
	
}
